package cn.iocoder.yudao.module.erp.dal.mysql.saleorder;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.saleorder.vo.SaleOrderPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.saleorder.SaleOrderDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 销售订单 Mapper
 * 销售订单和报价单是同一个表，通过type字段区分，这里拆分出两个模块的方法
 * @author maxuxu
 */
@Mapper
public interface SaleOrderMapper extends BaseMapperX<SaleOrderDO> {

    default PageResult<SaleOrderDO> selectPage(SaleOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SaleOrderDO>()
                .likeIfPresent(SaleOrderDO::getBillNo, reqVO.getBillNo())
                .eqIfPresent(SaleOrderDO::getBillContacter, reqVO.getBillContacter())
                .likeIfPresent(SaleOrderDO::getBillContacterName, reqVO.getBillContacterName())
                .eqIfPresent(SaleOrderDO::getCustomerOrderNo, reqVO.getCustomerOrderNo())
                .likeIfPresent(SaleOrderDO::getCustomerName, reqVO.getCustomerName())
                .likeIfPresent(SaleOrderDO::getProjectName, reqVO.getProjectName())
                .likeIfPresent(SaleOrderDO::getSettlementName, reqVO.getSettlementName())
                .eqIfPresent(SaleOrderDO::getSyncPay, reqVO.getSyncPay())
                .eqIfPresent(SaleOrderDO::getSyncOut, reqVO.getSyncOut())
                .eqIfPresent(SaleOrderDO::getSyncInvoice, reqVO.getSyncInvoice())
                .likeIfPresent(SaleOrderDO::getGoodsInfo, reqVO.getGoodsInfo())
                .betweenIfPresent(SaleOrderDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SaleOrderDO::getId));
    }
    /**
     * @param reqVO:
      * @return PageResult<SaleOrderDO>
     * @author Max-u-19
     * @description 销售订单的方法
     * @date 2024/1/24 20:38
     */
    default PageResult<SaleOrderDO> selectSaleOrderPage(SaleOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SaleOrderDO>()
                .likeIfPresent(SaleOrderDO::getBillNo, reqVO.getBillNo())
                .eqIfPresent(SaleOrderDO::getBillContacter, reqVO.getBillContacter())
                .likeIfPresent(SaleOrderDO::getBillContacterName, reqVO.getBillContacterName())
                .eqIfPresent(SaleOrderDO::getCustomerOrderNo, reqVO.getCustomerOrderNo())
                .likeIfPresent(SaleOrderDO::getCustomerName, reqVO.getCustomerName())
                .likeIfPresent(SaleOrderDO::getProjectName, reqVO.getProjectName())
                .likeIfPresent(SaleOrderDO::getSettlementName, reqVO.getSettlementName())
                .eqIfPresent(SaleOrderDO::getSyncPay, reqVO.getSyncPay())
                .eqIfPresent(SaleOrderDO::getSyncOut, reqVO.getSyncOut())
                .eqIfPresent(SaleOrderDO::getSyncInvoice, reqVO.getSyncInvoice())
                .likeIfPresent(SaleOrderDO::getGoodsInfo, reqVO.getGoodsInfo())
                .betweenIfPresent(SaleOrderDO::getCreateTime, reqVO.getCreateTime())
                .eq(SaleOrderDO::getType, 0)
                .orderByDesc(SaleOrderDO::getId));
    }
    default int insertSaleOrder(SaleOrderDO saleOrderDO) {
        saleOrderDO.setType(0);
        return insert(saleOrderDO);
    }
    /**
     * @param reqVO:
      * @return null
     * @author Max-u-19
     * @description 报价单的方法
     * @date 2024/1/24 20:41
     */
    default PageResult<SaleOrderDO> selectQuoteOrderPage(SaleOrderPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SaleOrderDO>()
                .likeIfPresent(SaleOrderDO::getBillNo, reqVO.getBillNo())
                .eqIfPresent(SaleOrderDO::getBillContacter, reqVO.getBillContacter())
                .likeIfPresent(SaleOrderDO::getBillContacterName, reqVO.getBillContacterName())
                .eqIfPresent(SaleOrderDO::getCustomerOrderNo, reqVO.getCustomerOrderNo())
                .likeIfPresent(SaleOrderDO::getCustomerName, reqVO.getCustomerName())
                .likeIfPresent(SaleOrderDO::getProjectName, reqVO.getProjectName())
                .likeIfPresent(SaleOrderDO::getSettlementName, reqVO.getSettlementName())
                .eqIfPresent(SaleOrderDO::getSyncPay, reqVO.getSyncPay())
                .eqIfPresent(SaleOrderDO::getSyncOut, reqVO.getSyncOut())
                .eqIfPresent(SaleOrderDO::getSyncInvoice, reqVO.getSyncInvoice())
                .betweenIfPresent(SaleOrderDO::getCreateTime, reqVO.getCreateTime())
                .betweenIfPresent(SaleOrderDO::getExpireTime,reqVO.getExpireTime())
                .eq(SaleOrderDO::getType, 1)
                .orderByDesc(SaleOrderDO::getId));
    }
    default int insertQuoteOrder(SaleOrderDO saleOrderDO) {
        saleOrderDO.setType(1);
        return insert(saleOrderDO);
    }


    /**
     * @param type:
      * @return List<SaleOrderDO>
     * @author Max-u-19
     * @description  type 0 销售订单 ， 1 报价单 获取所有销售订单
     * @date 2024/2/2 14:12
     */
    default List<SaleOrderDO> selectAmountTotal(Integer type){
        return selectList(new QueryWrapper<SaleOrderDO>()
                .eq("type",type)
                .select("total_amount"));
    }

    /**
     * @author Max-u-19
     * @Description 获取已、未收款订单的总额
     * @date 12:13 2024/11/29
     * @param
     * @return
     **/
    default SaleOrderDO selectAmountTotalBySync(Integer sync) {
        return selectOne(new QueryWrapper<SaleOrderDO>()
                .eq("sync_pay",sync)
                .select("total_amount"));
    }

    default SaleOrderDO selectByNo(String no) {
        return selectOne(new LambdaQueryWrapperX<SaleOrderDO>()
                .eq(SaleOrderDO::getBillNo, no));
    }
}